package com.hegongda.okcmall.product.service.impl;

import com.hegongda.okcmall.product.entity.BrandEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hegongda.common.utils.PageUtils;
import com.hegongda.common.utils.Query;

import com.hegongda.okcmall.product.dao.CategoryBrandRelationDao;
import com.hegongda.okcmall.product.entity.CategoryBrandRelationEntity;
import com.hegongda.okcmall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         *  查询某一品牌下的分类
         */
        String brandId = (String) params.get("brandId");
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(brandId))
            brandId = "-1";
        queryWrapper.eq("brand_id", brandId);
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     *  向品牌与分类表中插入数据
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        // 获取品牌名
        String brandName = baseMapper.getBrandName(brandId);
        // 获取分类名
        String categoryName = baseMapper.getCategoryName(catelogId);

        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryName);

        baseMapper.insert(categoryBrandRelation);
    }

    /**
     *  获取某一分类下的品牌id
     * @param catId
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> query(Long catId) {
        List<CategoryBrandRelationEntity> entities = baseMapper.
                selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        return entities;
    }

}